//
//  MainDetailViewController.m
//  Daybook
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "MainDetailViewController.h"
#import "MainDetailNoPicCell.h"
#import "Common.h"
#import "AccountBook.h"
#import "MainDetailHavePicCell.h"

#define BAR_WIDTH self.navigationController.navigationBar.frame.size.width
#define BAR_HEIGHT self.navigationController.navigationBar.frame.size.height

@interface MainDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)RLMResults *accountBooks;


@property (nonatomic, strong)NSMutableArray *dayCountArr;

@property (nonatomic, strong)NSMutableArray *moneyArr;


@property (nonatomic, strong)NSMutableArray *accountBooksArr;


@end

@implementation MainDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    [self createData];
}

- (void)createView {
    
    
    
    
    
    
    
    
//    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden = YES;
    //self.navigationController.navigationBar.alpha = 0.1;
//    
   // self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    //self.navigationController.navigationItem.backBarButtonItem.title = []
    
    
    

    
    
    
    
    
    

    
    
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = MAIN_HEIGHT;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.pagingEnabled = YES;
    self.tableView.contentOffset = CGPointMake(0,(self.count + self.index) *MAIN_HEIGHT);
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[MainDetailNoPicCell class] forCellReuseIdentifier:NSStringFromClass([MainDetailNoPicCell class])];
    
    [self.tableView registerClass:[MainDetailHavePicCell class] forCellReuseIdentifier:NSStringFromClass([MainDetailHavePicCell class])];
    
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 0, self.view.frame.size.width, 44 + 20);
    topView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:topView];
    
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, topView.frame.size.height - 34, 25, 25);
    [self.view addSubview:backButton];
    backButton.backgroundColor = [UIColor clearColor];
    [topView addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"backArrow"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
   
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    RLMResults *result = self.accountBooksArr[section];
    return result.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.accountBooksArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RLMResults *result = self.accountBooksArr[ indexPath.section];
    Account *accountBook = result[result.count - 1 - indexPath.row];
    
    
    if (accountBook.imageData == nil) {
        
        MainDetailNoPicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MainDetailNoPicCell class]) forIndexPath:indexPath];
        
        cell.accountBook = accountBook;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        return cell;
    }else {
        MainDetailHavePicCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MainDetailHavePicCell class]) forIndexPath:indexPath];
        
        cell.accountBook = accountBook;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        return cell;
    }
    
}


- (void)createData {
    
    
    RLMResults *timeInAccountBooks = [Account allObjects];
    NSMutableArray *timeArr= [timeInAccountBooks valueForKey:@"time"];
//    NSLog(@"%@", timeArr);
    NSMutableArray *dayArr = [NSMutableArray array];
    for (NSInteger i = 0; i < timeArr.count; i ++) {
        NSDate *date = [self extractDate:timeArr[i]];
        [dayArr addObject:date];
    }
//    NSLog(@"%@",dayArr);
    
    
    NSMutableSet *set = [NSMutableSet set];
    self.dayCountArr = [NSMutableArray array];
    for (NSInteger i = dayArr.count - 1; i >=0; i--) {
        [set addObject:dayArr[i]];
    }
//    NSLog(@"1111%@", set);
    
    for (NSDate *date in set) {
        [self.dayCountArr addObject:date];
    }
//    NSLog(@"111%@", self.dayCountArr);
    
    [self.dayCountArr sortUsingSelector:@selector(compare:)];
//    NSLog(@"是不是你%@", self.dayCountArr);
    
    
    
    
//    RLMRealm *realm = [RLMRealm defaultRealm];
    
    //    NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@", self.dayCountArr[0]]];
    //    NSLog(@"%@", [NSString stringWithFormat:@"%@", self.dayCountArr[0]]);
    //    NSDate *date1 = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@", self.dayCountArr[1]]];
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    
    
    
    self.moneyArr = [NSMutableArray array];
    self.accountBooksArr = [NSMutableArray array];
    for (NSInteger i = self.dayCountArr.count - 1; i >= 0; i--) {
        NSDate *nextDate = [NSDate dateWithTimeInterval: 24*60*60 sinceDate:self.dayCountArr[i]];
        
        self.accountBooks = [currentDayBook.bookContent objectsWhere:@"time >=  %@ AND time < %@ ", self.dayCountArr[i], nextDate];
        
//        self.accountBooks = [AccountBook objectsInRealm:realm where:@"time >=  %@ AND time < %@ ", self.dayCountArr[i], nextDate];
//        RLMResults *accountBooksTypeTwo = [AccountBook objectsInRealm:realm where:@"time >=  %@ AND time < %@ AND type = '2'", self.dayCountArr[i], nextDate];
        
        [self.accountBooksArr addObject:self.accountBooks];
        
//        self.sumMoney = [accountBooksTypeTwo sumOfProperty:@"money"];
//        [self.moneyArr addObject:self.sumMoney];
    }
    
    
    
    
    [self.tableView reloadData];


    
    
    
}

- (NSDate *)extractDate:(NSDate *)date {
    //get seconds since 1970
    NSTimeInterval interval = [date timeIntervalSince1970];
    int daySeconds = 24 * 60 * 60;
    //calculate integer type of days
    NSInteger allDays = interval / daySeconds;
    
    return [NSDate dateWithTimeIntervalSince1970:allDays * daySeconds];
}


- (void)clickBack:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
